package com.finance.capacity.deliveryDetail.capService;

import com.finance.base.enums.CapSiteDetailStatusEnum;
import com.finance.capacity.deliveryDetail.bean.pojo.DeliveryDetailErr;
import com.finance.capacity.deliveryDetail.bean.pojo.DeliveryDetailInfo;
import com.finance.capacity.deliveryDetail.bean.pojo.DeliveryDetailList;
import com.finance.capacity.deliveryDetail.capDao.DeliveryDetailMapper;
import com.finance.capacity.siteDetail.bean.pojo.SiteDetailList;
import com.finance.capacity.siteDetail.capDao.SiteDetailMapper;
import com.finance.capacity.siteDetailLog.bean.pojo.SiteDetailLog;
import com.finance.capacity.siteDetailLog.capDao.SiteDetailLogMapper;
import com.finance.system.bean.pojo.User;
import com.finance.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: ly
 * @Date: Created in 2017/8/24
 * @Description:
 */
@Service
public class DeliveryDetailService implements IDeliveryDetailService {
    @Autowired
    private DeliveryDetailMapper deliveryDetailMapper;
    @Autowired
    private SiteDetailLogMapper siteDetailLogMapper;
    @Autowired
    private SiteDetailMapper siteDetailMapper;

    @Override
    public List<DeliveryDetailList> getDeliveryList(String siteDetailId) {
        return deliveryDetailMapper.getDeliveryList(siteDetailId);
    }

    @Override
    public List<DeliveryDetailErr> summaryDeliveryModify(String deliverys,User user) {
        Long relationId = Long.valueOf("0") ;
        List<DeliveryDetailErr> deliveryDetailErrs = new ArrayList<>();
        String[] deliveryList = deliverys.split(",");
        for( String modifys : deliveryList ){
            String[] modifyList = modifys.split("&");
            DeliveryDetailInfo deliveryDetailInfo = deliveryDetailMapper.getDeliveryInfo(Long.valueOf(modifyList[0].trim()));
            if( deliveryDetailInfo == null ){
                DeliveryDetailErr deliveryDetailErr = new DeliveryDetailErr();
                deliveryDetailErr.setId(Long.valueOf("0"));
                deliveryDetailErr.setDeliveryName(modifyList[0].trim());
                deliveryDetailErr.setErrMsg("id为" + modifyList[0].trim() + "数据不存在，请联系系统管理员");
                deliveryDetailErrs.add(deliveryDetailErr);
                continue;
            }
            if(StringUtils.isNotBlank(String.valueOf(deliveryDetailInfo.getRelationId()))){
                SiteDetailList siteCapInfo = siteDetailMapper.getSiteCapInfo(String.valueOf(deliveryDetailInfo.getRelationId()));
                if("2".equals(siteCapInfo.getStatus())){
                    DeliveryDetailErr deliveryDetailErr = new DeliveryDetailErr();
                    deliveryDetailErr.setDeliveryName(deliveryDetailInfo.getDeliveryName());
                    deliveryDetailErr.setErrMsg("该记录处于已审核状态,不给修改");
                     continue;
                }
            }
            Integer modifyOrderEffiNum = deliveryDetailInfo.getOrderCommonNum() + deliveryDetailInfo.getDxOrderNumAdjust() - Integer.valueOf(modifyList[1].trim());
            if( modifyOrderEffiNum < 0 ){
                DeliveryDetailErr deliveryDetailErr = new DeliveryDetailErr();
                deliveryDetailErr.setId(deliveryDetailInfo.getId());
                deliveryDetailErr.setDeliveryName(deliveryDetailInfo.getDeliveryName());
                deliveryDetailErr.setErrMsg("修改后导致人效单量为负，与实际情况不符合");
                deliveryDetailErrs.add(deliveryDetailErr);
                continue;
            }
            DeliveryDetailList deliveryDetailList = new DeliveryDetailList();
            deliveryDetailList.setId(Long.parseLong(modifyList[0].trim()));
            deliveryDetailList.setBatchOrderNumModify( Integer.valueOf(modifyList[1]) );
            deliveryDetailList.setOrderEfficiencyNum(modifyOrderEffiNum);
            deliveryDetailList.setUpdateUid(user.getId());
            relationId = deliveryDetailInfo.getRelationId();
            int result = deliveryDetailMapper.modifyDelivery(deliveryDetailList);
            if( result <= 0 ){
                DeliveryDetailErr deliveryDetailErr = new DeliveryDetailErr();
                deliveryDetailErr.setId(deliveryDetailInfo.getId());
                deliveryDetailErr.setDeliveryName(deliveryDetailInfo.getDeliveryName());
                deliveryDetailErr.setErrMsg("更新数据失败");
                deliveryDetailErrs.add(deliveryDetailErr);
            }else{
                //新增log记录
                StringBuilder logInfo = new StringBuilder();
                logInfo.append(DateUtil.dateMinuteFormat()).append("  质控")
                        .append(user.getRealName()).append("修改")
                        .append("   ").append(deliveryDetailInfo.getDeliveryName()).append("  批量件：")
                        .append(deliveryDetailInfo.getBatchOrderNumModify()).append(" --> ").append(modifyList[1].trim())
                        .append("，人效单量：")
                        .append(deliveryDetailInfo.getOrderEfficiencyNum()).append(" --> ").append(modifyOrderEffiNum);
                SiteDetailLog siteDetailLog = new SiteDetailLog();
                siteDetailLog.setCreateUid(user.getId());
                siteDetailLog.setRelationId(deliveryDetailInfo.getRelationId());
                siteDetailLog.setModContent(logInfo.toString());
                result = siteDetailLogMapper.addSiteDetailLog(siteDetailLog);
            }
        }
        if( String.valueOf(relationId).compareTo("0") != 0 ){
            SiteDetailList siteDetailNew = siteDetailMapper.computeNewSiteDetail(relationId);
            SiteDetailList siteDetailList = siteDetailMapper.getSiteDetailById(relationId);
            if( siteDetailNew == null || siteDetailList == null ){
                DeliveryDetailErr deliveryDetailErr = new DeliveryDetailErr();
                deliveryDetailErr.setId(Long.valueOf("0"));
                deliveryDetailErr.setDeliveryName("站点");
                deliveryDetailErr.setErrMsg("未获取到当天站点信息，请联系系统管理员");
                deliveryDetailErrs.add(deliveryDetailErr);
            }
            siteDetailList.setId(relationId);
            siteDetailList.setUpdateUid(user.getId());
            siteDetailList.setOrderEfficiencyNum(siteDetailNew.getOrderCommonNum() + siteDetailNew.getDxOrderNumAdjust() - siteDetailNew.getBatchOrderNum());
            siteDetailList.setBatchOrderNum(siteDetailNew.getBatchOrderNum());
            siteDetailList.setStatus(CapSiteDetailStatusEnum.modify.getStatus());
            DecimalFormat df = new DecimalFormat("0.00");
            float siteEfficiency = Float.valueOf(String.valueOf(siteDetailList.getOrderEfficiencyNum())) / siteDetailList.getWorkerNum();
            siteDetailList.setSiteEfficiency(BigDecimal.valueOf(Double.valueOf(df.format(siteEfficiency))));
            int res = siteDetailMapper.updateSiteDetail(siteDetailList);
            if( res <= 0 ){
                DeliveryDetailErr deliveryDetailErr = new DeliveryDetailErr();
                deliveryDetailErr.setId(Long.valueOf("0"));
                deliveryDetailErr.setDeliveryName("站点");
                deliveryDetailErr.setErrMsg("更新此站点汇总数据失败");
                deliveryDetailErrs.add(deliveryDetailErr);
            }
        }
        return deliveryDetailErrs;
    }

    @Override
    public List<DeliveryDetailList> getCoeDeliveryList(String relationId) {
        return deliveryDetailMapper.getCoeDeliveryList(relationId);
    }


    @Override
    public List<DeliveryDetailErr> maintainDeliveryModify(String deliverys, User user) {
        Long updateId = user.getId();
        Long relationId = Long.valueOf("0") ;
        int modifyOrderNum = 0;
        List<DeliveryDetailErr> deliveryDetailErrs = new ArrayList<>();
        String[] deliveryList = deliverys.split(",");
        for( String modifys : deliveryList ){
            String[] modifyList = modifys.split("&");
//            deliveryDetailLists.add(deliveryDetailList);
            DeliveryDetailInfo deliveryDetailInfo = deliveryDetailMapper.getDeliveryInfo(Long.valueOf(modifyList[0].trim()));
            if( deliveryDetailInfo == null ){
                DeliveryDetailErr deliveryDetailErr = new DeliveryDetailErr();
                deliveryDetailErr.setId(Long.valueOf("0"));
                deliveryDetailErr.setDeliveryName(modifyList[0].trim());
                deliveryDetailErr.setErrMsg("id为" + modifyList[0].trim() + "数据不存在，请联系系统管理员");
                deliveryDetailErrs.add(deliveryDetailErr);
                continue;
            }
            Integer modifyNum = deliveryDetailInfo.getOrderCommonNum() + deliveryDetailInfo.getDxOrderNumAdjust() - Integer.valueOf(modifyList[1].trim());
            if( modifyNum < 0 ){
                DeliveryDetailErr deliveryDetailErr = new DeliveryDetailErr();
                deliveryDetailErr.setId(deliveryDetailInfo.getId());
                deliveryDetailErr.setDeliveryName(deliveryDetailInfo.getDeliveryName());
                deliveryDetailErr.setErrMsg("修改后人效单量为负，与实际不符");
                deliveryDetailErrs.add(deliveryDetailErr);
                continue;
            }
            DeliveryDetailList deliveryDetailList = new DeliveryDetailList();
            deliveryDetailList.setId(Long.parseLong(modifyList[0].trim()));
            deliveryDetailList.setBatchOrderNumModify( Integer.valueOf(modifyList[1]) );
            deliveryDetailList.setOrderEfficiencyNum(modifyNum);
            deliveryDetailList.setUpdateUid(updateId);
            relationId = deliveryDetailInfo.getRelationId();
            int result = deliveryDetailMapper.modifyDelivery(deliveryDetailList);
            if( result <= 0 ){
                DeliveryDetailErr deliveryDetailErr = new DeliveryDetailErr();
                deliveryDetailErr.setId(deliveryDetailInfo.getId());
                deliveryDetailErr.setDeliveryName(deliveryDetailInfo.getDeliveryName());
                deliveryDetailErr.setErrMsg("更新数据失败");
                deliveryDetailErrs.add(deliveryDetailErr);
            }else{
                modifyOrderNum = modifyOrderNum + deliveryDetailInfo.getBatchOrderNumModify() -  Integer.valueOf(modifyList[1].trim());
                //新增log记录
                StringBuilder logInfo = new StringBuilder();
                logInfo.append(DateUtil.dateMinuteFormat()).append("  站长")
                        .append(user.getRealName()).append("修改")
                        .append("   ").append(deliveryDetailInfo.getDeliveryName()).append("  批量件：")
                        .append(deliveryDetailInfo.getBatchOrderNumModify()).append(" --> ").append(modifyList[1].trim())
                        .append("，人效单量：")
                        .append(deliveryDetailInfo.getOrderEfficiencyNum()).append(" --> ").append(modifyNum);
                SiteDetailLog siteDetailLog = new SiteDetailLog();
                siteDetailLog.setCreateUid(updateId);
                siteDetailLog.setRelationId(deliveryDetailInfo.getRelationId());
                siteDetailLog.setModContent(logInfo.toString());
                result = siteDetailLogMapper.addSiteDetailLog(siteDetailLog);
            }
        }
        if( String.valueOf(relationId).compareTo("0") != 0 ){
            SiteDetailList siteDetailNew = siteDetailMapper.computeNewSiteDetail(relationId);
            SiteDetailList siteDetailList = siteDetailMapper.getSiteDetailById(relationId);
            if( siteDetailNew == null || siteDetailList == null ){
                DeliveryDetailErr deliveryDetailErr = new DeliveryDetailErr();
                deliveryDetailErr.setId(Long.valueOf("0"));
                deliveryDetailErr.setDeliveryName("站点");
                deliveryDetailErr.setErrMsg("未获取到当天站点信息，请联系系统管理员");
                deliveryDetailErrs.add(deliveryDetailErr);
            }
            siteDetailList.setId(relationId);
            siteDetailList.setUpdateUid(updateId);
            siteDetailList.setOrderEfficiencyNum(siteDetailNew.getOrderCommonNum() + siteDetailNew.getDxOrderNumAdjust() - siteDetailNew.getBatchOrderNum());
            siteDetailList.setBatchOrderNum(siteDetailNew.getBatchOrderNum());
            siteDetailList.setStatus(CapSiteDetailStatusEnum.modify.getStatus());
            DecimalFormat df = new DecimalFormat("0.00");
            float siteEfficiency = Float.valueOf(String.valueOf(siteDetailList.getOrderEfficiencyNum())) / siteDetailList.getWorkerNum();
            siteDetailList.setSiteEfficiency(BigDecimal.valueOf(Double.valueOf(df.format(siteEfficiency))));
            int res = siteDetailMapper.updateSiteDetail(siteDetailList);
            if( res <= 0 ){
                DeliveryDetailErr deliveryDetailErr = new DeliveryDetailErr();
                deliveryDetailErr.setId(Long.valueOf("0"));
                deliveryDetailErr.setDeliveryName("站点");
                deliveryDetailErr.setErrMsg("更新此站点汇总数据失败");
                deliveryDetailErrs.add(deliveryDetailErr);
            }
        }
        return deliveryDetailErrs;
    }

    @Override
    public List<DeliveryDetailList> deliveryCapDetail(String relationId) {
        return deliveryDetailMapper.deliveryCapDetail(relationId);
    }
}



